package com.kermel.andromeda.activities.countdown.add;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.kermel.andromeda.R;
import com.kermel.andromeda.data.CountdownTimer;
import com.kermel.andromeda.data.TimeRemaining;
import com.kermel.andromeda.engine.CountdownSubscriber;
import com.kermel.andromeda.engine.CountdownTicker;
import com.kermel.andromeda.scheduler.CountdownTimerScheduler;
import com.kermel.andromeda.scheduler.CountdownTimerScheduler.OnFinishListener;
import com.kermel.common.util.StringUtils;

public class CountdownTimerActivity extends Activity implements CountdownSubscriber {
    
    public static final int COUNTDOWN_TIMER_ACTIVITY_RESULT_ID = CountdownTimerActivity.class.getName().hashCode();
    
    private TextView countdownTimeRemainingDisplay;
    private TextView countdownTimerVibrateWhenTimerEndsDisplay;
    private TextView countdownTimerPlaySoundWhenTimerEndsDisplay;
    private TextView countdownTimerSoundTitleDisplay;
    private View countdownTimerCancelTimerButton;
    
    private CountdownTicker countdownTicker;
    private CountdownTickUpdater countdownTickUpdater;
    private CountdownTimerScheduler scheduler;
    private CountdownTimer countdownTimer;
    private int hours;
    private int minutes;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.countdown_timer);
        
        storeWidgetHandles();
        
        retrieveBundleExtraData(savedInstanceState);
        
        updateDisplayBasedOnValuesFromCountdownTimer();
        
        initListeners();
        
        initCountdownTicker();
        
        initCountdownTickUpdater();
        
        scheduleCountdownTimer();
    }
    
    private void storeWidgetHandles() {
        countdownTimeRemainingDisplay = (TextView)findViewById(R.id.countdownTimerRemainingDisplay);
        countdownTimerVibrateWhenTimerEndsDisplay = (TextView)findViewById(R.id.countdownTimerVibrateWhenTimerEndsDisplay);
        countdownTimerPlaySoundWhenTimerEndsDisplay = (TextView)findViewById(R.id.countdownTimerPlaySoundWhenTimerEndsDisplay);
        countdownTimerSoundTitleDisplay = (TextView)findViewById(R.id.countdownTimerSoundTitleDisplay);
        countdownTimerCancelTimerButton = findViewById(R.id.countdownTimerCancelTimerButton);
    }
    
    private void retrieveBundleExtraData(Bundle savedInstanceState) {
        if (savedInstanceState == null) {
            Intent intent = getIntent();
            
            if (intent != null) {
                Bundle bundle = intent.getExtras();
                
                if (bundle != null)
                    countdownTimer = bundle.getParcelable(CountdownTimer.COUNTDOWN_TIMER_PARCEL_NAME);
            }
        }
    }
    
    private void updateDisplayBasedOnValuesFromCountdownTimer() {
        if (countdownTimer != null) {
            updateWhenTimerEndsChoices();
            updateSoundTitleDisplay(countdownTimer.ringtoneTitle);
        }
    }
    
    private void updateWhenTimerEndsChoices() {
        updatePlaySoundWhenTimerEnds();
        updateVibrateWhenTimerEnds();
    }
    
    private void updatePlaySoundWhenTimerEnds() {
        if (countdownTimer.hasRingtone())
            displayPlaySoundWhenTimerEnds();
        else
            clearPlaySoundWhenTimerEndsDisplay();
    }
    
    private void displayPlaySoundWhenTimerEnds() {
        String text = getString(R.string.playSound);
        countdownTimerPlaySoundWhenTimerEndsDisplay.setText(text);
        countdownTimerPlaySoundWhenTimerEndsDisplay.setVisibility(View.VISIBLE);
    }
    
    private void clearPlaySoundWhenTimerEndsDisplay() {
        countdownTimerPlaySoundWhenTimerEndsDisplay.setText(StringUtils.EMPTY);
        countdownTimerPlaySoundWhenTimerEndsDisplay.setVisibility(View.GONE);
    }
    
    private void updateVibrateWhenTimerEnds() {
        if (countdownTimer.vibrate)
            displayVibrateWhenTimerEnds();
        else
            clearVibrateWhenTimerEndsDisplay();
    }
    
    private void displayVibrateWhenTimerEnds() {
        String text = getString(R.string.vibrateDevice);
        countdownTimerVibrateWhenTimerEndsDisplay.setText(text);
        countdownTimerVibrateWhenTimerEndsDisplay.setVisibility(View.VISIBLE);
    }
    
    private void clearVibrateWhenTimerEndsDisplay() {
        countdownTimerVibrateWhenTimerEndsDisplay.setText(StringUtils.EMPTY);
        countdownTimerVibrateWhenTimerEndsDisplay.setVisibility(View.GONE);
    }
    
    private void updateSoundTitleDisplay(String title) {
        countdownTimerSoundTitleDisplay.setText(title);
    }
    
    private void initListeners() {
        countdownTimerCancelTimerButton.setOnClickListener(new CancelCountdownTimerAction());
    }
    
    private void initCountdownTicker() {
        countdownTicker = new CountdownTicker(this);
        countdownTicker.start(hours, minutes);
    }
    
    private void initCountdownTickUpdater() {
        countdownTickUpdater = new CountdownTickUpdater();
    }
    
    private void scheduleCountdownTimer() {
        scheduler = new CountdownTimerScheduler(countdownTimer);
        
        CountdownTimerFinishedAction listener = new CountdownTimerFinishedAction();
        scheduler.setOnFinishListener(listener);
        
        scheduler.start();
    }
    
    public void onCountdownTick(TimeRemaining timeRemaining) {
        String timeRemainingString = timeRemaining.toFormattedString();
        countdownTickUpdater.updateCountdownTime(timeRemainingString);
    }
    
    public void onCountdownFinished() {
        // Since we're using the Android CountDownTimer we don't really care about CountdownSubscriber's notification that the countdown has finished.
    }
    
    private void onCancel() {
        cancelScheduledCountdownTimer();
        finish();
    }
    
    private void cancelScheduledCountdownTimer() {
        if (scheduler != null)
            scheduler.cancel();
    }
    
    private void exitActivityWhenCountdownTimerHasFinished() {
        finish();
    }
    
    @Override
    protected void onDestroy() {
        cancelScheduledCountdownTimer();
        super.onDestroy();
    }
    
    private class CancelCountdownTimerAction implements View.OnClickListener {
        public void onClick(View v) {
            onCancel();
        }
    }
    
    private class CountdownTickUpdater implements Runnable {
        private String timeRemainingString;
        
        public void updateCountdownTime(String timeRemainingString) {
            this.timeRemainingString = timeRemainingString;
            countdownTimeRemainingDisplay.post(this);
        }
        
        public void run() {
            countdownTimeRemainingDisplay.setText(timeRemainingString);
        }
    }
    
    private class CountdownTimerFinishedAction implements OnFinishListener {
        public void onFinish() {
            exitActivityWhenCountdownTimerHasFinished();
        }
    }
}
